package com.xiaodou.mjf.web.console.basic.utils;


import com.xiaodou.mjf.application.basic.service.MenuService;
import com.xiaodou.mjf.entity.basic.Menu;
import com.xiaodou.mjf.infrastructure.common.context.SpringContextHolder;
import com.xiaodou.mjf.infrastructure.common.utils.StringUtils;

/**
 * Created by Rocky on 14/12/21.
 */
public class NavigationUtils {

    static MenuService menuService = SpringContextHolder.getBean(MenuService.class);

    public static String title(String menuId){

        String template = "<h3 class='page-title'>%s <small>%s</small></h3>";

        Menu menu = menuService.get(menuId);
        if(menu != null){
            template = String.format(template,menu.getName(),menu.getDescription());
        }else{
            template = String.format(template,"","");
        }

        return template;
    }

    public static String breadcrumb(String menuId){

        Menu menu = menuService.get(menuId);

        /*
        * <ul class="breadcrumb">

							<li><i class="icon-home"></i> <a href="index.html">首页</a>
							<i class="icon-angle-right"></i></li>

							<li><a href="#">概览</a></li>


						</ul>
        * */


        StringBuffer sb = new StringBuffer();
        String contextPath = SpringContextHolder.getApplicationContext().getApplicationName();
        
        sb.append("<ul class=\"breadcrumb\">");
        sb.append("<li><i class=\"icon-home\"></i> <a href=\"");
        if(StringUtils.isNotBlank(contextPath)){           
            sb.append(contextPath);
        }
        sb.append("\">");
        sb.append("首页");
        sb.append("</a>");
        sb.append("<i class=\"icon-angle-right\"></i></li>");

        if(!menu.getParent().getId().equals(Menu.ROOT_ID)){
            sb.append("<li><a href=\"javascript:;\"><i class=\"");
            sb.append(menu.getParent().getIcon());
            sb.append("\">");
            sb.append("</i>");
            sb.append(menu.getParent().getName());
            sb.append("</a><i class=\"icon-angle-right\"></i></li>");
        }

        sb.append("<li><a href=\"\"");
        
        if(StringUtils.isNotBlank(contextPath)){
            sb.append("/");
            sb.append(contextPath);
        }
        sb.append(menu.getUrl());
        sb.append("\">");
        sb.append(menu.getName());
        sb.append("</a></li>");

        return sb.toString();
    }
}
